package com.qf.smartplatform.controller;

import com.qf.smartplatform.dto.R;
import com.qf.smartplatform.log.annotations.LogAnnotation;
import com.qf.smartplatform.pojo.SysCategory;
import com.qf.smartplatform.service.CategoryService;
import com.qf.smartplatform.valigroup.insert;
import com.qf.smartplatform.valigroup.update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/category")
public class CategoryController {
    private CategoryService categoryService;
    @Autowired
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @PostMapping("/add")
    @LogAnnotation(title = "添加分类",business_type = 1)
    public R addCategory(@Validated(insert.class) @RequestBody SysCategory sysCategory){
        categoryService.addCategory(sysCategory);
        return R.OK();
    }
    @PostMapping("")
    @LogAnnotation(title = "查询所有分类",business_type = 0)
    public R findAllCategory(){
        return R.OK(categoryService.findAllCategories());
    }
    @GetMapping("/find/{cId}")
    @LogAnnotation(title = "根据分类id查询分类",business_type = 0)
    public R findCategoryById(@PathVariable Long cId){
        return R.OK(categoryService.findCategoryById(cId));
    }
    @GetMapping("/delete/{cId}")
    @LogAnnotation(title = "根据分类id删除分类",business_type = 3)
    public R deleteCategoryById(@PathVariable Long cId){

        return R.OK(categoryService.deleteCategoryById(cId));
    }
    @PostMapping("/update")
    @LogAnnotation(title = "更新分类",business_type = 2)
    public R updateCategoryById(@Validated(update.class)@RequestBody SysCategory sysCategory){
        return R.OK(categoryService.updateCategory(sysCategory));
    }
}
